package com.ygqh.baby.service.impl;

import com.wdt.api.WdtClient;
import com.wdt.api.WdtResponse;
import com.wdt.api.exception.WdtException;
import com.wdt.api.request.GoodsPushRequest;
import com.wdt.api.request.support.GoodsDTO;
import com.wdt.api.request.support.GoodsPushModel;
import com.wdt.api.request.support.GoodsSpec;
import com.ygqh.baby.po.AdminSku;
import com.ygqh.baby.service.WdtQybGoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: sunshuo
 * @date: 2019/2/27 14:17
 */
@Service
public class WdtQybGoodsServiceImpl implements WdtQybGoodsService {

    @Autowired
    private WdtClient wdtClient;

    @Override
    public WdtResponse goodsPush(AdminSku adminSku) throws WdtException {
        return goodsPush(Collections.singletonList(adminSku));
    }

    @Override
    public WdtResponse goodsPush(List<AdminSku> adminSkuList) throws WdtException {
        validateParamBeforePush(adminSkuList);

        GoodsPushModel goodsPushModel = buildGoodsPushModel(adminSkuList);
        GoodsPushRequest request = new GoodsPushRequest();
        request.setBizModel(goodsPushModel);

        return wdtClient.execute(request);
    }

    private GoodsPushModel buildGoodsPushModel(List<AdminSku> adminSkuList) {
        Map<String, List<AdminSku>> collect = adminSkuList.stream()
                .collect(Collectors.groupingBy(AdminSku::getProductCode));
        List<GoodsDTO> goodsList = new ArrayList<>();
        collect.forEach((productCode, list) -> {
            GoodsDTO goods = new GoodsDTO();
            goods.setGoodsNo(productCode);
            goods.setGoodsType(1);
            AdminSku sku1 = list.get(0);
            goods.setGoodsName(sku1.getProductName());
            if (StringUtils.isNotBlank(sku1.getConcatCategoryName())) {
                goods.setClassName(sku1.getConcatCategoryName());
            }
            goods.setBrandName("Yiigoo");
            List<GoodsSpec> goodsSpecList = new ArrayList<>();
            for (AdminSku sku : list) {
                GoodsSpec spec = new GoodsSpec();
                spec.setSpecNo(sku.getSkuCode());
                spec.setSpecCode("");
                spec.setBarcode(sku.getSkuCode());
                spec.setSpecName(sku.getColor() + sku.getSizeName());
                goodsSpecList.add(spec);
            }
            goods.setSpecList(goodsSpecList);
            goodsList.add(goods);
        });

        GoodsPushModel goodsPushModel = new GoodsPushModel();
        goodsPushModel.setGoodsList(goodsList);
        return goodsPushModel;
    }

    private void validateParamBeforePush(List<AdminSku> skuList) {
        Assert.notEmpty(skuList, "skuList can't be empty");
        for (AdminSku sku : skuList) {
            Assert.isTrue(StringUtils.isNotBlank(sku.getProductCode()), "productCode can't be blank");
            Assert.isTrue(StringUtils.isNotBlank(sku.getProductName()), "productName can't be blank");
            Assert.isTrue(StringUtils.isNotBlank(sku.getSkuCode()), "skuCode can't be blank");
        }
    }
}
